package com.rocket.controller;

import com.rocket.param.TeduClass;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SendController {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    //http://localhost:8081/send?className=JSD2307
    @GetMapping("/send")
    public String send(TeduClass teduClass){
        // 发送消息 消息体 携带数据就是teduClass
        //1. 准别一个消息对象 palLoad=body序列化之前
        Message<TeduClass> message = MessageBuilder.withPayload(teduClass).build();
        //2. 调用发送方法 syncSend asyncSend
        //rocketMQTemplate.syncSend("test-topic-02",message);
        SendResult sendResult = rocketMQTemplate.syncSend("test-topic-02", message, 500, 4);
        rocketMQTemplate.asyncSend("test-topic-02", message, new SendCallback() {
            // 异步线程接收反馈 消息发送成功
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println("消息发送成功");
            }
            // 异步线程接收反馈 消息发送失败
            @Override
            public void onException(Throwable throwable) {
                System.out.println("消息发送失败");
            }
        });
        return "success";
    }
}
